# usage: add-staduims-to-db.py input_file stad_name stad_place stad_team

import sys, os, re

def CreateSynList(synList, fullName):
    synListOut = ""
    synList.append(fullName)
    for item in synList:
        if (item != ""):
            synListOut += "\"%s\" " % item
        
    return synListOut

def ExtractValue(line):
    line = line.strip("\n")
    line = re.sub("\s*{{[^}]+}}\s*", "\t", line)
    line = re.sub("\s*\[\[[^\]]+\]\]\s*", "\t", line)
    line = re.sub("«", "", line)
    line = re.sub("»", "", line)
    line = re.sub("–", "", line)
    line = re.sub("—", "", line)
    #line = re.sub("\s*\([^\)]+\)\s*", "\t", line)
    line = re.sub("'", "", line)
    line = re.sub("\t*\|.*$", "", line)
    #line = re.sub("\|", "", line)
    #fields = line.split("\t")
    #fields = [x for x in fields if x]
    #if (nField < len(fields)):
        #return fields[nField]
    line = line.replace("&lt;br /&gt;", " ")
    line = line.replace("&lt;BR&gt;", " ")
    return line.split("\t")[-1:][0]

if (len(sys.argv) < 3):
    print ("usage: add-staduims-to-db.py input_file stad_name stad_place stad_team")
    sys.exit(1)

i = 0
with open (sys.argv[1]) as stad_name:
    with open (sys.argv[2]) as stad_place:
        with open(sys.argv[3]) as stad_team:
            for line in stad_name:
                line_place = stad_place.readline()
                line_team = stad_team.readline()
                
                line = line[:line.find(" (")]
                line_place = line_place[:line_place.find("\t[")]
                line_team = line_team[:line_team.find("\t[")]
                line_team = line_team[:line_team.find(" (")]
                
                stadName = ExtractValue(line)
                stadPlace = ExtractValue(line_place)
                stadTeam = ExtractValue(line_team)
                
                if (stadName == ""):
                    continue
                
                os.system("add-stadium.py \"%s\" \"%s\" \"%s\"" % (stadName, stadPlace, stadTeam))